package de.tum.in.eist.poll.adapter;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

import de.tum.in.eist.poll.controller.FeedbackController;
import de.tum.in.eist.poll.controller.LectureController;
import de.tum.in.eist.poll.controller.UserController;
import de.tum.in.eist.poll.shared.Lecture;


/**
 * ViewCreator handles the creation and setup of all views
 * @author sudi
 *
 */
public class ViewCreator {
	public Button logoutButton = new Button("Logout");
	public Button backButton = new Button("Back");
	
	/**
	 * creates the logout and back controll buttons
	 * @param userController The userController to link the logout button
	 */
	public void create_controls(final UserController userController) {
		RootPanel.get("back_button").add(backButton);
		backButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				History.back();
			}
			
		});
		
		RootPanel.get("logout_button").add(logoutButton);
		logoutButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				logoutButton.removeFromParent();
				backButton.removeFromParent();
				userController.logout();
			}
			
		});
	}
	
	/**
	 * Creates the login view
	 * @param userController The userController to link the login behaviour
	 */
	public void createLogin(UserController userController) {
		RootPanel.get().clear();
		
		LoginAdapter login = new LoginAdapter(userController);
		login.create();
	}
	
	/**
	 * Creates the user view with its lectures
	 * @param lectureController The lecture Controller to have acces to the users lectures
	 * 
	 */
	public void createUserView(LectureController lectureController) {
		RootPanel.get().clear();
		
		UserViewAdapter userView = new UserViewAdapter(lectureController);
		userView.create();
		userView.loadLectures();
	}
	
	/**
	 * The Feedback view
	 * @param feedbackController The FeedbackController to submit feedback
	 */
	public void createLectureFeddbackView(FeedbackController feedbackController) {
		FeedbackAdapter feedbackView = new FeedbackAdapter(feedbackController);
		feedbackView.create();
	}
	
	/**
	 * The lecture view
	 * @param lectureId the Lecture id to load in the correct lecture
	 * @param lectureController The LectureController for lecture access
	 * @param feedbackController The FeedbackController to display and submit feedback
	 */
	public void createLectureView(String lectureId, LectureController lectureController, FeedbackController feedbackController) {
		RootPanel.get().clear();
		
		String[] parts = lectureId.split("_");
		int id = Integer.parseInt(parts[1]);
		
		List<Lecture> lectures = lectureController.getLectures();
		
		if (lectures.isEmpty() || lectures.size() <= id) {
			History.newItem("user");
			return;
		}
		
		lectureController.setSelected(id);
		LectureAdapter lectureView = new LectureAdapter(lectureController, feedbackController);
		lectureView.create();
	}
}
